package com.china08.yxyapi.api.manage.edu.platform.schooltuijian;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.service.sws.EdbWebGroupService;

@CrossOrigin
@RestController
public class EduSchoolTuiJianListController extends YxyApiBaseController {

    @Autowired
    EdbWebGroupService edbWebGroupService;

    @RequestMapping(value = "/api_yw/manage/edu/platform/schooltuijian/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {

        String provinceId = reqModel.provinceId;
        String cityId = reqModel.cityId;
        String districtId = reqModel.districtId;
        String schType = reqModel.schType;
        String keyword = StringUtils.isNotBlank(reqModel.keyword) ? "%" + reqModel.keyword + "%" : "";
        boolean tuijian = reqModel.tuijian;
        Integer pageNum = reqModel.pageNum;
        Integer bureauLevel = reqModel.bureauLevel;

        Integer allPage = 0;
        List<Map<String, Object>> list = new ArrayList<>();
        List<Pojo> schoolList = new ArrayList<Pojo>();
        Map<String, Object> map = edbWebGroupService.EduSchoolTuiJianList(pageNum, bureauLevel, provinceId, cityId, districtId, schType, keyword, tuijian);
        if (map != null) {
            allPage = Integer.parseInt(map.get("allPage") + "");
            list = (List<Map<String, Object>>) map.get("list");
        }

        for (Map<String, Object> m : list) {
            Pojo pojo = new Pojo();
            pojo.schoolId = m.get("school_id") + "";
            pojo.schoolNick = m.get("school_name") + "";
            pojo.schoolAddress = m.get("address") + "";
            Object webObject = m.get("web_url");
            if (webObject != null) {
                pojo.schoolWebUrl = "http://" + m.get("web_url") + "";
            }

            List<String> schoolTypeList = new ArrayList<>();
            // 是否是幼儿园
            Object object0 = m.get("is_st0");
            // 是否是小学
            Object object1 = m.get("is_st1");
            // 是否是初中
            Object object2 = m.get("is_st2");
            // 是否是高中
            Object object3 = m.get("is_st3");
            if (object0 != null && (boolean) object0) {
                schoolTypeList.add("幼儿园");
            }
            if (object1 != null && (boolean) object1) {
                schoolTypeList.add("小学");
            }
            if (object2 != null && (boolean) object2) {
                schoolTypeList.add("初中");
            }
            if (object3 != null && (boolean) object3) {
                schoolTypeList.add("高中");
            }
            pojo.schoolType = StringUtils.join(schoolTypeList.toArray(), ",");

            // 省推荐
            Object obj1 = m.get("is_recommend_province");
            // 市推荐
            Object obj2 = m.get("is_recommend_city");
            // 县推荐
            Object obj3 = m.get("is_recommend_district");
            pojo.recommendProvince = (bureauLevel == 2 && obj1 != null && (boolean) obj1);
            pojo.recommendCity = (bureauLevel == 1 && obj2 != null && (boolean) obj2);
            pojo.recommendDistrict = (bureauLevel == 0 && obj3 != null && (boolean) obj3);
            schoolList.add(pojo);
        }

        RespModel respModel = new RespModel();
        respModel.allPage = allPage;
        respModel.schoolList = schoolList;
        return respModel;
    }

    public static class ReqModel extends YxyApiReqModel {
        public boolean tuijian;
        public Integer bureauLevel;
        public String provinceId;
        public String cityId;
        public String districtId;
        public String schType;
        public String keyword;
        public Integer pageNum;

        @Override
        public void autoCheck() {
        }

    }

    public static class RespModel extends YxyApiRespModel {
        public Integer allPage;
        public List<Pojo> schoolList;
    }

    public static class Pojo {
        public String schoolId;
        public String schoolNick;
        public String schoolType;
        public String schoolAddress;
        public String schoolWebUrl;
        public boolean recommendProvince = false;
        public boolean recommendCity = false;
        public boolean recommendDistrict = false;
    }
}
